package com.yisoo.ubag.product.service.impl;

import com.yisoo.common.utils.R;
import com.yisoo.ubag.product.entity.BrandEntity;
import com.yisoo.ubag.product.entity.ConBraSceEntity;
import com.yisoo.ubag.product.service.BrandService;
import com.yisoo.ubag.product.service.ConBraSceService;
import com.yisoo.ubag.product.vo.TransferBrandDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yisoo.common.utils.PageUtils;
import com.yisoo.common.utils.Query;

import com.yisoo.ubag.product.dao.SceneDao;
import com.yisoo.ubag.product.entity.SceneEntity;
import com.yisoo.ubag.product.service.SceneService;
import org.springframework.util.StringUtils;


@Service("sceneService")
public class SceneServiceImpl extends ServiceImpl<SceneDao, SceneEntity> implements SceneService {

    @Autowired
    BrandService brandService;

    @Autowired
    ConBraSceService conBraSceService;



    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String) params.get("key");
        Long cid = Long.valueOf((String)params.get("cid"));

        QueryWrapper<SceneEntity> queryWrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(key)){

            queryWrapper.eq("scene_id",key).or().like("scene_name",key);
        }

        queryWrapper.eq("parent_id",cid);

        IPage<SceneEntity> page = this.page(
                new Query<SceneEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public R getBrandRelated(Long cid, Long sid) {
        //先查出所有品牌
        List<TransferBrandDataVo> allBrandList= brandService.getEnableBrandByCid(cid);
        //再从关系表中查出现有关系
        List<TransferBrandDataVo> okBrandList = conBraSceService.getEnableBrandByCidAndSid(cid, sid);

//前端需要所有的数据
//        //过滤掉已选中的元素
//        List<TransferBrandDataVo> noBrandList = allBrandList.stream().filter(temp ->{
//            if(okBrandList.contains(temp)){
//                return false;
//            }else{
//                return true;
//            }
//        }).collect(Collectors.toList());
        //优化数据传输 只传前面要用的
        List<Long> rightList = okBrandList.stream().map(temp->temp.getKey()).collect(Collectors.toList());
        return R.ok().put("left",allBrandList).put("right",rightList);
    }

    @Override
    public R setBrandRelated(Long cid, Long sid, ArrayList<Integer> value) {
        SceneEntity sceneEntity = baseMapper.selectById(sid);
        //收集提交的结果
        List<ConBraSceEntity> submitList = value.stream().map(bid->{

            return new ConBraSceEntity(null,cid,Long.parseLong(String.valueOf(bid)),brandService.getById(bid).getBrandName(),sid,sceneEntity.getSceneName());
        }).collect(Collectors.toList());
        //查看表里的结果
        List<ConBraSceEntity> haveList = conBraSceService.getHaveList(cid,sid)
                .stream().map(temp->{
                    temp.setTempId(null);
                    return temp;
                }).collect(Collectors.toList());



        List<ConBraSceEntity> jiaoList = new ArrayList<>();
        jiaoList.addAll(submitList);
        //交集 这部分不需要更新
        jiaoList.retainAll(haveList);

        //需要提交的
        submitList.removeAll(jiaoList);
        //需要删除的
        haveList.removeAll(jiaoList);


        //加入
//        System.out.println("------------------------");
        for (ConBraSceEntity temp :
                submitList) {
//            System.out.println(temp);
            conBraSceService.save(temp);
        }
//        删除
//        System.out.println("------------------------");
        for (ConBraSceEntity temp :
                haveList) {
//            System.out.println(temp);
            QueryWrapper<ConBraSceEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("consumer_id",cid)
                    .eq("scene_id",sid)
                    .eq("brand_id",temp.getBrandId());
            conBraSceService.remove(queryWrapper);
        }
//        System.out.println("------------------------");
//        for (ConBraSceEntity temp :
//                jiaoList) {
//            System.out.println(temp);
//        }
        return R.ok();
    }

    @Override
    public Integer getNumByParentId(Long userid) {
        QueryWrapper<SceneEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",userid);
        return  baseMapper.selectList(queryWrapper).size();
    }

}